#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/if.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kernel_stat.h>
#include <linux/platform_device.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
#include <net/neighbour.h>
#include <net/arp.h>
#include <linux/if_arp.h>

/**
 *  sudo  arp-scan -I eth0  --localnet
 */

struct device *dev;
#define HBUFFERLEN 30

void cb(struct neighbour *nb, void *a) {
	int k, j;
	char tbuf[16];
	char hbuffer[HBUFFERLEN];
	struct net_device *dev = nb->dev;
	int hatype = dev->type;
	int flags = nb->flags;

	pr_info("flags, %d\n", flags);

	if ((flags & IFF_LOOPBACK) == IFF_LOOPBACK) {
		pr_info("IFF_LOOPBACK\n");
	}

	if (ARPHRD_LOOPBACK == hatype)
		return;
//	pr_info("hatype: %d\n", hatype);
//	pr_info("dev->name %s\n", nb->dev->name);
	sprintf(tbuf, "%pI4", nb->primary_key);
	pr_info("primary_key, %s\n", tbuf);
	pr_info("dev->addr_len, %d\n", dev->addr_len);
	for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < dev->addr_len; j++) {
		hbuffer[k++] = hex_asc_hi(nb->ha[j]);
		hbuffer[k++] = hex_asc_lo(nb->ha[j]);
		hbuffer[k++] = ':';
	}
	if (k != 0)
		--k;
	hbuffer[k] = 0;

	pr_info("mac, %s\n", hbuffer);
}

int myprobe(struct platform_device *pdev) {
	int cookie = 0;
	dev = &pdev->dev;
	pr_info("myplatformdriver myprobe \n");
	neigh_for_each(&arp_tbl, cb, &cookie);
	return 0;
}

int myremove(struct platform_device *pdev) {
	pr_info("myplatformdriver myremove \n");
	return 0;
}

struct of_device_id my_of_match_table =
		{ .compatible = "my_platform_device_003", };

struct platform_driver my_platform_driver = { .driver =
		{ .of_match_table = &my_of_match_table, .name = "my-platform-driver",
				.owner = THIS_MODULE, }, .probe = myprobe, .remove = myremove, };

module_platform_driver(my_platform_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");
